/*
 * Copyright (c) 2010, okazoh_tk. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  - Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *  - Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *  - Neither the name of the copyright holder nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef ALKES_GFXFILTER_GRAPHICS_H_INCLUDED_
#define ALKES_GFXFILTER_GRAPHICS_H_INCLUDED_

#include "alkes/gfxfilter/internal.h"
#include "alkes/gfx/IGraphics3DDevice.h"

AL_GF_EXPORT_API alkes::IGraphicsDevice* alkes_create_graphics_proxy(alkes::IGraphicsDevice* device, int is_3d);

namespace alkes {

/// \addtogroup gfxfilter
//@{

enum LogFilter
{
    AL_FILTER_IGraphicsDeviceflip = 0,
    AL_FILTER_IGraphicsDeviceresize,
    AL_FILTER_IGraphicsDevicecreateDecoderFactory,
    AL_FILTER_IGraphicsDevicecreateImageBufferFactory,

    AL_FILTER_IGraphics2DDevicecreateShape,
    AL_FILTER_IGraphics2DDevicecreateLineShape,
    AL_FILTER_IGraphics2DDevicecreateRectangleShape,
    AL_FILTER_IGraphics2DDevicecreateImageShape,
    AL_FILTER_IGraphics2DDevicecreatePolylineShape,
    AL_FILTER_IGraphics2DDevicecreateDefaultRenderTarget,
    AL_FILTER_IGraphics2DDevicebeginDraw2D,
    AL_FILTER_IGraphics2DDeviceendDraw2D,
    AL_FILTER_IGraphics2DDevicegetTargetRect,
    AL_FILTER_IGraphics2DDevicegetRenderTarget,
    AL_FILTER_IGraphics2DDevicesetScissorRect,
    AL_FILTER_IGraphics2DDevicegetScissorRect,
    AL_FILTER_IGraphics2DDevicesetState,
    AL_FILTER_IGraphics2DDevicegetState,
    AL_FILTER_IGraphics2DDevicesetAlphaBlendMode,
    AL_FILTER_IGraphics2DDevicegetAlphaBlendMode,
    AL_FILTER_IGraphics2DDevicesetTextureFilter,
    AL_FILTER_IGraphics2DDevicegetTextureFilter,
    AL_FILTER_IGraphics2DDevicesetTextureAddressMode,
    AL_FILTER_IGraphics2DDevicegetTextureAddressMode,
    AL_FILTER_IGraphics2DDevicefill,
    AL_FILTER_IGraphics2DDevicedrawShape,
    AL_FILTER_IGraphics2DDevicepushMatrix,
    AL_FILTER_IGraphics2DDevicepopMatrix,
    AL_FILTER_IGraphics2DDevicetranslate,
    AL_FILTER_IGraphics2DDevicetranslateForImage,
    AL_FILTER_IGraphics2DDevicescale,
    AL_FILTER_IGraphics2DDevicerotate,
    AL_FILTER_IGraphics2DDeviceskewX,
    AL_FILTER_IGraphics2DDeviceskewY,
    AL_FILTER_IGraphics2DDeviceonSnapshot,

    AL_FILTER_IGraphics3DDevicecreateVertexBuffer,
    AL_FILTER_IGraphics3DDevicesetViewport,
    AL_FILTER_IGraphics3DDevicegetViewport,
    AL_FILTER_IGraphics3DDevicesetPerspective,
    AL_FILTER_IGraphics3DDevicesetLookAt,
    AL_FILTER_IGraphics3DDevicetranslate,
    AL_FILTER_IGraphics3DDevicescale,
    AL_FILTER_IGraphics3DDevicerotate,
    AL_FILTER_IGraphics3DDeviceclearBuffer,
    AL_FILTER_IGraphics3DDevicedrawPrimitive,
    AL_FILTER_IGraphics3DDevicesetTexture,
    AL_FILTER_IGraphics3DDevicesetTextureFilter,
    AL_FILTER_IGraphics3DDevicegetTextureFilter,
    AL_FILTER_IGraphics3DDevicesetTextureAddressMode,
    AL_FILTER_IGraphics3DDevicegetTextureAddressMode,
    AL_FILTER_IGraphics3DDevicecreateVertexShader,
    AL_FILTER_IGraphics3DDevicecreateFragmentShader,
    AL_FILTER_IGraphics3DDevicecreateShaderProgram,
    AL_FILTER_IGraphics3DDevicesetShaderProgram,


    AL_FILTER_MAX,
};



/** Decorator class of IGraphics3D interface.
*/
class AL_GF_EXPORT GfxFilterGraphics
    : public IGraphics3DDevice
{
    AL_DECLARE_FILTER_CLASS(GfxFilterGraphics, IGraphics3DDevice);

public:
    static GfxFilterGraphics* create(IGraphics2DDevice* component);
    static GfxFilterGraphics* create(IGraphics3DDevice* component);

    virtual void resize(uint16_t width, uint16_t height);
    virtual void flip();
    virtual DecoderFactory* createDecoderFactory();
    virtual ImageBufferFactory* createImageBufferFactory();
    //virtual IVertexBuffer* createVertexBuffer(uint32_t vertex_type);

    void enableAllLogFilter();
    void disableAllLogFilter();
    void enableLogFilter(LogFilter filter);
    void disableLogFilter(LogFilter filter);
    bool isEnableLogFilter(LogFilter filter) const;

    virtual Shape* createShape(int32_t shape_type, uint32_t num);
    virtual LineShape* createLineShape(uint32_t num);
    virtual RectangleShape* createRectangleShape(uint32_t num);
    virtual ImageShape* createImageShape(uint32_t num);
    virtual PolylineShape* createPolylineShape(uint32_t num);
    virtual RenderTarget* createDefaultRenderTarget();
    virtual void beginDraw2D(RenderTarget* target);
    virtual void endDraw2D();
    virtual const Rect getTargetRect() const;
    virtual RenderTarget* getRenderTarget();
    virtual void setScissorRect(const Rect& rect);
    virtual const Rect getScissorRect() const;
    virtual void setState(int state, bool enable);
    virtual bool getState(int state);
    virtual void setAlphaBlendMode(AlphaBlendMode src_blend, AlphaBlendMode dst_blend);
    virtual void getAlphaBlendMode(AlphaBlendMode* src_blend, AlphaBlendMode* dst_blend) const;
    virtual void setTextureFilter(TexFilterValue min_filter, TexFilterValue mag_filter);
    virtual void getTextureFilter(TexFilterValue* min_filter, TexFilterValue* mag_filter);
    virtual void setTextureAddressMode(TexAddressMode u_mode, TexAddressMode v_mode);
    virtual void getTextureAddressMode(TexAddressMode* u_mode, TexAddressMode* v_mode);
    virtual void fill(const Color& color);
    virtual bool drawShape(Shape* shape, uint32_t start = 0, uint32_t count = 1);
    virtual bool drawShape(LineShape* line_shape, uint32_t start = 0, uint32_t count = 1);
    virtual bool drawShape(RectangleShape* rectangle_shape, uint32_t start = 0, uint32_t count = 1);
    virtual bool drawShape(ImageShape* image_shape, uint32_t start = 0, uint32_t count = 1);
    virtual bool drawShape(PolylineShape* polyline_shape, uint32_t start = 0, uint32_t count = 1);
    virtual void pushMatrix();
    virtual void popMatrix();
    virtual void translate(const Vector2D& offset);
    virtual void translate(float x, float y);
    virtual void translateForImage(float x, float y);
    virtual void scale(float xscale, float yscale, const Vector2D& origin = Vector2D::ZERO);
    virtual void scale(float scaling, const Vector2D& origin = Vector2D::ZERO);
    virtual void rotate(float angle, const Vector2D& origin = Vector2D::ZERO);
    virtual void skewX(float angle);
    virtual void skewY(float angle);
    virtual void onSnapshot(ImageWriter& writer, const Rect& rect);



    virtual IVertexBuffer* createVertexBuffer(uint32_t vertex_type);
    virtual void setViewport(const Viewport& viewport);
    virtual const Viewport getViewport() const;
    virtual void setPerspective(float angle, float aspect, float znear, float zfar);
    virtual void setLookAt(const Vector3D& eye, const Vector3D& at, const Vector3D& up);
    virtual void translate(const Vector3D& offset);
    virtual void translate(float x, float y, float z);
    virtual void scale(float xscale, float yscale, float zscale);
    virtual void rotate(float angle, float x, float y, float z);
    virtual void clearBuffer(uint32_t buffer_flag, const Color& clear_color, float depth = -1.0f);
    virtual bool drawPrimitive(IVertexBuffer* vbuffer, PrimitiveType type, uint32_t start, uint32_t count);
    virtual bool setTexture(Image* image, int stage = 0);
    virtual void setTextureFilter(TexFilterValue min_filter, TexFilterValue mag_filter, int stage);
    virtual void getTextureFilter(TexFilterValue* min_filter, TexFilterValue* mag_filter, int stage);
    virtual void setTextureAddressMode(TexAddressMode u_mode, TexAddressMode v_mode, int stage);
    virtual void getTextureAddressMode(TexAddressMode* u_mode, TexAddressMode* v_mode, int stage);
    virtual Shader* createVertexShader(const char* source);
    virtual Shader* createFragmentShader(const char* source);
    virtual Program* createShaderProgram(Shader* vertex_shader, Shader* fragment_shader);
    virtual Program* createShaderProgram(const char* vertex_shader_source, const char* fragment_shader_source);
    virtual bool setShaderProgram(Program* shader_program);

private:
    GfxFilterGraphics(IGraphics2DDevice* component);
    GfxFilterGraphics(IGraphics3DDevice* component);
    virtual ~GfxFilterGraphics();

    virtual void destroy();

private:
    intrusive_ptr<IGraphics3DDevice> component_;
    uint32_t log_filter_[4];
    bool is_3d_;
};

#if !defined(__GNUC__)
#define AL_LOG_INVOKED0(classname, method, format)  \
    AL_DBG("Invoked: ((%s*)0x%p)->%s " format, #classname, static_cast<void*>(component_), #method)
#define AL_LOG_INVOKED(classname, method, format, ...)  \
    AL_DBG("Invoked: ((%s*)0x%p)->%s " format, #classname, static_cast<void*>(component_), #method, __VA_ARGS__)
#else
#define AL_LOG_INVOKED0(classname, method, format)  \
    AL_DBG("Invoked: ((%s*)0x%p)->%s " format, #classname, static_cast<void*>(component_), #method)
#define AL_LOG_INVOKED(classname, method, format, ...)  \
    AL_DBG("Invoked: ((%s*)0x%p)->%s " format, #classname, static_cast<void*>(component_), #method, ##__VA_ARGS__)
#endif

#define AL_LOG_BEGIN_PARAM(type, is_array)  AL_DBG("Define: " #type " temporary %s = {", is_array ? "[]" : "")
#if !defined(__GNUC__)
#define AL_LOG_PARAM(format, ...)           AL_DBG("Define: " format, __VA_ARGS__)
#else
#define AL_LOG_PARAM(format, ...)           AL_DBG("Define: " format, ##__VA_ARGS__)
#endif
#define AL_LOG_END_PARAM()                  AL_DBG("Define: };")

#if !defined(__GNUC__)
#define AL_LOG_RETVAL(type, format, ...)    AL_DBG("Retval: (%s)" format, #type, __VA_ARGS__)
#else
#define AL_LOG_RETVAL(type, format, ...)    AL_DBG("Retval: (%s)" format, #type, ##__VA_ARGS__)
#endif

//@}

}

#endif
